package cn.pegasus.teamwork.appinterface.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;

/**
 * 结果返回
 * @author Administrator
 *
 */
public class ReturnRsUtils {

	private String code;
	
	private String msg;
	
	private Object data;
	
	private Boolean rs;
	
	

	/**
	 * @return the rs
	 */
	public Boolean getRs() {
		return rs;
	}

	/**
	 * @param rs the rs to set
	 */
	public void setRs(Boolean rs) {
		this.rs = rs;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the data
	 */
	public Object getData() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public void setData(Object data) {
		this.data = data;
	}
	
	/**
     * 用于客户端解析的json对象
     *
     * @param paraEunm 枚举对象 eg:ParameterEunm.SUCCESSFUL_CODE
     * @param paraData 返回的具体数据 可对象为空
     * @return 封装好的返回对象
     */
    public static Map<String, Object> resultBack(ParameterEunm paraEunm, Object paraData) {
        Map<String, Object> re = new HashMap<String, Object>();
        re.put(ParameterEunm.RESULT_DATA_STATUS_KEY.getBackParameter().toString(), paraEunm.getBackParameter());
        if (null != paraData) {
            re.put(ParameterEunm.RESULT_DATA_CONTENT_KEY.getBackParameter().toString(), paraData);
        }
        re.put(ParameterEunm.RESULT_DATA_MESSAGE_KEY.getBackParameter().toString(), paraEunm.getParameterName());
        return re;
    }
	
    /**
     * 用于客户端解析的json对象
     *
     * @param paraEunm
     *            枚举对象 eg:ParameterEunm.SUCCESSFUL_CODE
     * @param paraData
     *            返回的具体数据 可对象为空
     * @return 封装好的返回对象
     */
    public static Map<String, Object> resultBackString(ParameterEunm paraEunm, Object paraData) {
        Map<String, Object> re = new HashMap<String, Object>();
        re.put(ParameterEunm.RESULT_DATA_STATUS_KEY.getBackParameter().toString(), paraEunm.getBackParameter());
        if (null != paraData) {
        	ValueFilter filter = new ValueFilter() {
    		    public Object process(Object obj, String s, Object v) {
    		    	if(v==null || v.equals("null"))
	    		        return "";
	    		    return v;
    		    }
    		};
    		String str = JSON.toJSONString(paraData, filter);
    		JSONObject json = new JSONObject();
    		if((!StringUtils.isEmpty(str) && str.equals("true")) || (!StringUtils.isEmpty(str) &&  str.equals("false"))) {
    			Map<String,Object> m = new HashMap<String,Object>();
    			m.put("rs", str);
    			str = JSON.toJSONString(m, filter);
    			json = JSON.parseObject(str);
    		}else {    			
    			json = JSON.parseObject(str);
    		}
    		System.out.println("----->"+json);
            re.put(ParameterEunm.RESULT_DATA_CONTENT_KEY.getBackParameter().toString(), json);
        }
        re.put(ParameterEunm.RESULT_DATA_MESSAGE_KEY.getBackParameter().toString(), paraEunm.getParameterName());
        return re;
    }
}
